﻿using System;
using System.Net;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;
using suanpan.API.Components.Exceptions;


namespace suanpan.API.Components.Behaviors.ErrorHandling
{
    class RestErrorBehavior : IErrorHandler
    {
        #region IErrorHandler Members

        public bool HandleError(Exception error)
        {
            return false;
        }

        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            RESTException restEx = error as RESTException;
            if (restEx != null)
            {
                fault = new RestErrorMessage(restEx);
            }
            else
            {
                fault = new RestErrorMessage(HttpStatusCode.InternalServerError, error.ToString(),string.Empty);
                WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = false;
            }

            
        }

        #endregion
    }
}
